package com.opencvjava.lessons.core;

import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.springframework.stereotype.Service;

import static com.opencvjava.support.util.CommonUtils.cost;
import static com.opencvjava.support.util.CvUtils.imshow;
import static com.opencvjava.support.util.CvUtils.mat;
import static org.opencv.imgproc.Imgproc.*;
import static org.opencv.imgcodecs.Imgcodecs.*;
import static org.opencv.core.Core.*;

@Service
public class L5_OperationsWithImages {
    public void test() {
        Mat src = mat("lena.png");
        if (src.empty()) {
            throw new RuntimeException("读取失败");
        }
        Mat dst = new Mat();
        src.convertTo(dst, CvType.CV_32F);
        long start = System.nanoTime();
        Mat gray = new Mat();
        cvtColor(src, gray, COLOR_BGR2GRAY);
        Mat sobelx = new Mat();
        Sobel(gray, sobelx, CvType.CV_32F, 1, 0);
        cost("sobelx", start);
        MinMaxLocResult res = minMaxLoc(sobelx);
        Mat draw = new Mat();
        double maxVal = res.maxVal, minVal = res.minVal;
        // 很有用的技巧
        sobelx.convertTo(draw, CvType.CV_8U, 255.0 / (maxVal - minVal), -minVal * 255.0 / (maxVal - minVal));
        cost("sobelx数值优化:", start);
        imshow("draw2", draw);
    }
}
